package com.ybfenpan.service.core;

import com.ybfenpan.dao.core.BaseDao;
import com.ybfenpan.model.core.BaseModel;
import com.ybfenpan.taglib.pagetag.Pagination;
import com.ybfenpan.utils.MapUtil;
import com.ybfenpan.utils.StringUtil;
import com.ybfenpan.utils.SysConst;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BaseService {

	@Resource
	private BaseDao baseDao;
	
	public BaseDao getBaseDao(){
		return baseDao;
	}
	
	/**
	 * 根据主键查询对象
	 * @param key key
	 * @return T
	 * @author: Qiyh 2013-12-24
	 */
	public <T>T getByKey(Object key){
		return (T)queryForObject("getByKey", key);
	}
	
	/**
	 * 保存对象(修改或添加)
	 * @return id
	 */
	public int saveOrUpdate(Object t){
		int id = 0;
		if(t instanceof Map){
			Map map = (Map)t;
			id = MapUtil.getIntValue(map, "id");
		}
		else if(t instanceof BaseModel){
			BaseModel bm = (BaseModel)t;
			id = bm.getId();
		}
		if(0<id && isExist(t)){
			update(t);
		}else{
			Integer i = (Integer)create(t);
			
			if(i != null){
				id = i.intValue();
			}
		}
		return id;
	}
	
	/**
	 * 添加对象
	 * @param obj obj
	 * @return Object
	 * @author: Qiyh 2013-12-24
	 */
	public Object create(Object obj){
		return createInfo("insert", obj);
	}
	
	/**
	 * 添加对象
	 * @param sqlId sqlId
	 * @param paramObj paramObj
	 * @return Object
	 * @author: Qiyh 2013-12-24
	 */
	public Object createInfo(String sqlId, Object paramObj){
		if(sqlId.indexOf(".")<0){
			sqlId = getSqlMapNameSpace() + sqlId;
		}
		return baseDao.createInfo(sqlId, paramObj);
	}

	/**
	 * 批量添加对象
	 * @param list list
	 * @return boolean
	 * @author: Qiyh 2013-12-24
	 */
	public boolean createBatch(List<?> list){
		if(list!=null && 0<list.size()){
			String className = list.get(0).getClass().getSimpleName();
			String listVar = StringUtil.toLowerCaseFirstOne(className)+"s";
			HashMap insertMap = new HashMap();
			insertMap.put(listVar, list);
			createInfo("insertBatch", insertMap);
		}
		return false;
	}

	
	/**
	 * 修改对象
	 * @param obj obj
	 * @return boolean
	 * @author: Qiyh 2013-12-24
	 */
	public  boolean update(Object obj){
		return updateInfo("update", obj);
	}
	
	
	
	
	
	/**
	 * 修改对象
	 * @param sqlId sqlId
	 * @param paramObj paramObj
	 * @return boolean
	 * @author: Qiyh 2013-12-24
	 */
	public boolean updateInfo(String sqlId, Object paramObj){
		if(sqlId.indexOf(".")<0){
			sqlId = getSqlMapNameSpace() + sqlId;
		}
		return baseDao.updateInfo(sqlId, paramObj);
	}
	
	/**
	 * 自定义修改
	 * @param obj obj
	 * @return boolean
	 * @author: Qiyh 2013-12-24
	 */
	public Object sumSql(Object obj){
		return sumSqlInfo("sumSql", obj);
	}
	
	
	/**
	 * 修改对象
	 * @param sqlId sqlId
	 * @param paramObj paramObj
	 * @return boolean
	 * @author: Qiyh 2013-12-24
	 */
	public Object sumSqlInfo(String sqlId, Object paramObj){
		if(sqlId.indexOf(".")<0){
			sqlId = getSqlMapNameSpace() + sqlId;
		}
		return baseDao.sumInfo(sqlId, paramObj);
	}
	/**
     * 批量更新
     * @param sqlId
     * @param list
     * @return
     */
    public int updateBatch(String sqlId, List<?> list) {
    	if(sqlId.indexOf(".")<0){
			sqlId = getSqlMapNameSpace() + sqlId;
		}
    	int i = 0;
    	SqlSession sqlSession = baseDao.getSqlSession();
        for(int cnt = list.size(); i < cnt; i++) {  
        	sqlSession.update(sqlId, list.get(i));  
            if((i + 1) % SysConst.BATCH_EXE_NUM == 0) {  
            	sqlSession.flushStatements();  
            }  
        }  
        sqlSession.flushStatements();  
        sqlSession.close();  
    	return i;
    }
	
	/**
	 * 删除对象
	 * @param obj obj
	 * @return boolean
	 * @author: Qiyh 2013-12-24
	 */
	public boolean delete(Object obj){
		return deleteInfo("delete", obj);
	}

	/**
	 * 删除对象(id)
	 * @param obj obj
	 * @return boolean
	 * @author: Qiyh 2013-12-24
	 */
	public boolean deleteById(Object obj){
		return deleteInfo("delete", obj);
	}



	/**
	 * 删除对象
	 * @param sqlId sqlId
	 * @param paramObj paramObj
	 * @return boolean
	 * @author: Qiyh 2013-12-24
	 */
	public boolean deleteInfo(String sqlId, Object paramObj){
		if(sqlId.indexOf(".")<0){
			sqlId = getSqlMapNameSpace() + sqlId;
		}
		return baseDao.deleteInfo(sqlId, paramObj);
	}
	
	/**
	 * 查询对象
	 * @param sqlId sqlId
	 * @param paramObj paramObj
	 * @return Object
	 * @author: Qiyh 2013-12-24
	 */
	public <T>T queryForObject(String sqlId, Object paramObj){
		if(sqlId.indexOf(".")<0){
			sqlId = getSqlMapNameSpace() + sqlId;
		}
		return (T)baseDao.queryForObject(sqlId, paramObj);
	}
	
	/**
	 * 查询列表
	 * @param sqlId sqlId
	 * @param paramObj paramObj
	 * @return List
	 * @author: Qiyh 2013-12-24
	 */
	public List queryForList(String sqlId, Object paramObj){
		if(sqlId.indexOf(".")<0){
			sqlId = getSqlMapNameSpace() + sqlId;
		}
		return baseDao.queryForList(sqlId, paramObj);
	}
	
	/**
	 * 查询列表分页(默认:paginate)
	 * 1) resultClass="java.util.HashMap"<br>
	 * 2) resultMap="XXX"(需 property="pageControlData.resultCount" column="resultCount" nullValue="0")
	 * @param paramObj paramObj
	 * @return List
	 * @author: Qiyh 2013-12-24
	 */
	public Pagination queryForPageList(Map<String, Object> paramObj){
		return queryForPageList("paginate", paramObj);
	}
	
	/**
	 * 查询列表分页<br>
	 * SQL返回:<br>
	 * 1) resultClass="java.util.HashMap"<br>
	 * 2) resultMap="XXX"(需 property="pageControlData.resultCount" column="resultCount" nullValue="0")
	 * @param sqlId sqlId
	 * @param paramObj paramObj
	 * @return Pagination
	 * @author: Qiyh 2013-12-24
	 */
	public Pagination queryForPageList(String sqlId, Map<String, Object> paramObj){
		if(sqlId.indexOf(".") < 0){
			sqlId = getSqlMapNameSpace() + sqlId;
		}
		return baseDao.queryForPageList(sqlId, paramObj);
	}
	
	/**
	 * 检查对象是否存在
	 * @param paramObj
	 * @return boolean
	 * @author: Qiyh 2013-12-24
	 */
	public boolean isExist(Object paramObj){
		return isExist("getByKey", paramObj);
	}
	
	/**
	 * 检查对象是否存在
	 * @param sqlId sqlId
	 * @param paramObj paramObj
	 * @return boolean
	 * @author: Qiyh 2013-12-24
	 */
	public boolean isExist(String sqlId, Object paramObj){
		boolean flag = false;
		List dataList = queryForList(sqlId, paramObj);
		if (null!=dataList && 0<dataList.size()) {
			flag = true;
		}
		return flag;
	}
	
	/**
	 * 执行存储过程/函数
	 * @param procId procId
	 * @param obj obj
	 * @author qiyh 2013-12-24
	 */
	public void execProc(String procId, Object obj){
		if(procId.indexOf(".")<0){
			procId = getSqlMapNameSpace() + procId;
		}
		baseDao.execProc(procId, obj);
	}
	
	/**
     * 获取sqlId所在sqlMap的namespace<br>
     *    默认为service名字的前缀,允许子类重载
     * @return String
     * @author qiyh 2013-12-24
     */
	protected String getSqlMapNameSpace(){
		String className = this.getClass().getSimpleName();
	    int idx = className.toLowerCase().indexOf("service");
	    if (0 < idx) {
	        return className.substring(0,idx) + ".";
        }
	    return "";
	}


}
